package cn.zsc.netdisk.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * @author ccying
 * @date 2021/2/4
 */
public class SendSmsUtil {
    private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

    /**
     * 发送手机验证码
     * @param phone 手机号码
     * @return 验证码
     * */
    public static int send(final String phone) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(Url);
        client.getParams().setContentCharset("UTF-8");
        method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
        int mobileCode = (int)((Math.random()*9+1)*100000);
        String content = new String("您的验证码是：" + mobileCode + "。请不要把验证码泄露给其他人。");
        NameValuePair[] data = {
            new NameValuePair("account", "C98882849"),
            new NameValuePair("password", "7816f91face49372adfac5f5e08e6eec"),
            new NameValuePair("mobile", phone),
            new NameValuePair("content", content),
        };
        method.setRequestBody(data);
        try {
            client.executeMethod(method);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
        return mobileCode;
    }
}
